#!/bin/bash

set -e

ROOT="$PWD"
PREFIX="${PREFIX:-$ROOT/build/local}"
echo "PREFIX: $PREFIX"

# Setup up folders
export PATH="$PREFIX/bin":$PATH
export LDFLAGS="-L$PREFIX/lib"
export LT_SYS_LIBRARY_PATH="$PREFIX/lib"
export DYLD_LIBRARY_PATH="$PREFIX/lib"
export LD_LIBRARY_PATH="$PREFIX/lib"
export LD_RUN_PATH="$PREFIX/lib"

# Download Google Test
export GTEST_VERSION=1.10.0
GTEST_DIR="$ROOT/build/local/src/gtest"
mkdir -p "$GTEST_DIR"
cd "$GTEST_DIR"
if [ ! -f release-$GTEST_VERSION.tar.gz ]; then
    curl -fSsOL https://github.com/google/googletest/archive/release-$GTEST_VERSION.tar.gz
fi
tar xzf release-$GTEST_VERSION.tar.gz

# Build gtest
cd googletest-release-$GTEST_VERSION
echo "patching cmake minimum version"
cp $ROOT/tools/*.patch .
patch CMakeLists.txt gtest.patch
patch googletest/CMakeLists.txt gtest_test.patch
patch googlemock/CMakeLists.txt gtest_mock.patch

cmake -DCMAKE_INSTALL_PREFIX:PATH=$PREFIX -H.
make -j4
make install
make clean

# Download Check
export CHECK_VERSION=0.15.2
CHECK_DIR="$ROOT/build/local/src/check"
mkdir -p "$CHECK_DIR"
cd "$CHECK_DIR"
if [ ! -f check-$CHECK_VERSION.tar.gz ]; then
    curl -fSsOL https://github.com/libcheck/check/releases/download/$CHECK_VERSION/check-$CHECK_VERSION.tar.gz
fi
tar xzf check-$CHECK_VERSION.tar.gz

# Build Check
cd check-$CHECK_VERSION
./configure --prefix="$PREFIX"
make -j4
make install
make clean

# Download Nlohmann JSON
export JSON_VERSION=3.9.1
JSON_DIR="$ROOT/build/local/json"
mkdir -p "$JSON_DIR"
cd "$JSON_DIR"
if [ ! -f include.zip ]; then
    curl -fSsOL https://github.com/nlohmann/json/releases/download/v$JSON_VERSION/include.zip
fi
unzip -d "$PREFIX" -o include.zip

# Download Protobuf sources
export PROTOBUF_VERSION=3.14.0
PROTOBUF_DIR="$ROOT/build/local/src/protobuf"
mkdir -p "$PROTOBUF_DIR"
cd "$PROTOBUF_DIR"
if [ ! -f protobuf-java-$PROTOBUF_VERSION.tar.gz ]; then
    curl -fSsOL https://github.com/protocolbuffers/protobuf/releases/download/v$PROTOBUF_VERSION/protobuf-java-$PROTOBUF_VERSION.tar.gz
fi
tar xzf protobuf-java-$PROTOBUF_VERSION.tar.gz

# Build Protobuf
cd protobuf-$PROTOBUF_VERSION
./configure --prefix="$PREFIX"
make -j4
make install
# after install, cleanup to save space (docker)
make clean
"$PREFIX/bin/protoc" --version

if [[ -x "$(command -v swift)" && `uname` == "Darwin" ]]; then
    # Download Swift Protobuf sources
    export SWIFT_PROTOBUF_VERSION=1.13.0
    SWIFT_PROTOBUF_DIR="$ROOT/build/local/src/swift-protobuf"
    mkdir -p "$SWIFT_PROTOBUF_DIR"
    cd "$SWIFT_PROTOBUF_DIR"
    if [ ! -f $SWIFT_PROTOBUF_VERSION.tar.gz ]; then
        curl -fSsOL https://github.com/apple/swift-protobuf/archive/$SWIFT_PROTOBUF_VERSION.tar.gz
    fi
    tar xzf $SWIFT_PROTOBUF_VERSION.tar.gz

    # Build Swift Protobuf
    cd swift-protobuf-$SWIFT_PROTOBUF_VERSION
    swift build --static-swift-stdlib -c release
    cp -f "$SWIFT_PROTOBUF_DIR/swift-protobuf-$SWIFT_PROTOBUF_VERSION/.build/release/protoc-gen-swift" "$PREFIX/bin" | true
    $PREFIX/bin/protoc-gen-swift --version
fi

# Protobuf plugins
cd "$ROOT/protobuf-plugin"
cmake -H. -Bbuild -DCMAKE_INSTALL_PREFIX=$PREFIX
make -Cbuild -j12
make -Cbuild install
rm -rf build

cd "$ROOT"
